<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  
  <title>【Git】修改已经提交的commit内容 | 程序小工  | Just do it!</title>
  <meta name="description" content="遇见更好的自己~" />
  <meta name="keywords" content="" />
  <meta name="HandheldFriendly" content="True" />
  <meta name="apple-mobile-web-app-capable" content="yes">
  <link rel="shortcut icon" href="/">
  <link rel="alternate" href="/atom.xml" title="程序小工  | Just do it!">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="通过 Git 进行版本管理时，对于已经提交但没有 push 的 message 信息，发现提交信息填写错误后，如何进行修改？ 对于已经 push 的 message 信息如何修改？通过git rebase -i进行分支管理，以及重新操作已经提交的分支信息[reword,edit,squash 等]。此次用到的主要是reword修改已经提交的message信息。">
<meta name="keywords" content="git">
<meta property="og:type" content="article">
<meta property="og:title" content="【Git】修改已经提交的commit内容">
<meta property="og:url" content="http://blog.i2arch.tech/2018/08/13/git-modify-commit-message/index.html">
<meta property="og:site_name" content="程序小工  | Just do it!">
<meta property="og:description" content="通过 Git 进行版本管理时，对于已经提交但没有 push 的 message 信息，发现提交信息填写错误后，如何进行修改？ 对于已经 push 的 message 信息如何修改？通过git rebase -i进行分支管理，以及重新操作已经提交的分支信息[reword,edit,squash 等]。此次用到的主要是reword修改已经提交的message信息。">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-08-15T08:57:06.160Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="【Git】修改已经提交的commit内容">
<meta name="twitter:description" content="通过 Git 进行版本管理时，对于已经提交但没有 push 的 message 信息，发现提交信息填写错误后，如何进行修改？ 对于已经 push 的 message 信息如何修改？通过git rebase -i进行分支管理，以及重新操作已经提交的分支信息[reword,edit,squash 等]。此次用到的主要是reword修改已经提交的message信息。">
    
  <link href="https://fonts.googleapis.com/css?family=Inconsolata|Titillium+Web" rel="stylesheet">
  <link href="https://fonts.googleapis.com/css?family=Roboto+Mono" rel="stylesheet">
  <link href='//cdn.bootcss.com/node-waves/0.7.5/waves.min.css' rel='stylesheet'>
  <link rel="stylesheet" href="/style.css">
  <script>
    function setLoadingBarProgress(num) {
      document.getElementById('loading-bar').style.width=num+"%";
    }
  </script>
</head>

<body>
  <div id="loading-bar-wrapper">
  <div id="loading-bar"></div>
</div>


  <script>setLoadingBarProgress(20)</script>
  <header class="l_header">
	<div class='wrapper'>
		<div class="nav-main container container--flex">
			<a class="logo flat-box" href='/' >
				程序小工  | Just do it!
			</a>
			<div class='menu'>
				<ul class='h-list'>
					
						<li>
							<a class='flat-box nav-home' href='/'>
								<i class='icon icon-list2'></i>
								首页
							</a>
						</li>
					
						<li>
							<a class='flat-box nav-archives' href='/archives'>
								<i class='icon icon-bookmark'></i>
								归档
							</a>
						</li>
					
						<li>
							<a class='flat-box nav-about' href='/about'>
								<i class='icon icon-user'></i>
								关于
							</a>
						</li>
					
				</ul>
				<div class='underline'></div>
			</div>
			
				<div class="m_search">
					<form name="searchform" class="form u-search-form">
						<input type="text" class="input u-search-input" placeholder="Search" />
						<span class="icon icon-search"></span>
					</form>
				</div>
			
			<ul class='switcher h-list'>
				
					<li class='s-search'><a href='javascript:void(0)'><span class="icon icon-search flat-box"></span></a></li>
				
				<li class='s-menu'><a href='javascript:void(0)'><span class="icon icon-menu flat-box"></span></a></li>
			</ul>
		</div>

		<div class='nav-sub container container--flex'>
			<a class="logo" class="flat-box" href='javascript:void(0)'>
				Word of Forks
			</a>

			<ul class='switcher h-list'>
				<li class='s-comment'><a href='javascript:void(0)'><span class="icon icon-chat_bubble_outline flat-box"></span></a></li>
				<li class='s-top'><a href='javascript:void(0)'><span class="icon icon-arrow_upward flat-box"></span></a></li>
				<li class='s-toc'><a href='javascript:void(0)'><span class="icon icon-format_list_numbered flat-box"></span></a></li>
			</ul>
		</div>
	</div>
</header>
<aside class="menu-phone">
	<nav>
		
			<a href="/" class="nav-home nav">
				首页
			</a>
		
			<a href="/archives" class="nav-archives nav">
				归档
			</a>
		
			<a href="/about" class="nav-about nav">
				关于
			</a>
		
	</nav>
</aside>

    <script>setLoadingBarProgress(40);</script>
  <div class="l_body">
    <div class='container clearfix'>
      <!-- 左侧内容 -->
      <div class='l_main'>
        <article id="post-git-modify-commit-message"
  class="post white-box article-type-post"
  itemscope itemprop="blogPost">
	<section class='meta'>

	<h2 class="title">
  	<a href="/2018/08/13/git-modify-commit-message/">
    	【Git】修改已经提交的commit内容
    </a>
	</h2>

	<div class="pagenav">
		<time>
			2018-08-13
		</time>

		
    
    <div class='cats'>
        <i class="icon icon-bookmark"></i>
        <a href="/categories/Git/">Git</a>
    </div>


		
			<div class="pagetags">
				<i class="icon icon-price-tag" style="margin-right: 2px;"></i>
				
					<a href="/tags/git/" style="margin-left: 3px;">
						git
					</a>
				
			</div>
			
	</div>
	</section>

	
	<section class="toc-wrapper"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#一-修改已经-commit-但没有-push-的-commit-message"><span class="toc-text"> 一、修改已经 commit 但没有 push 的 commit message</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1查看提交历史"><span class="toc-text"> 1.查看提交历史</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2通过git-rebase-i编辑提交的历史"><span class="toc-text"> 2.通过git rebase -i编辑提交的历史</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#二-修改已经-push-的-commmit-message"><span class="toc-text"> 二、修改已经 push 的 commmit message</span></a></li></ol></section>
	

	<section class="article typo">
		<div class="article-entry" itemprop="articleBody">
				<span class="zhaiyao">[摘要]</span>
			<p>通过 Git 进行版本管理时，对于已经提交但没有 push 的 message 信息，发现提交信息填写错误后，如何进行修改？<br>
对于已经 push 的 message 信息如何修改？通过<code>git rebase -i</code>进行分支管理，以及重新操作已经提交的分支信息[reword,edit,squash 等]。此次用到的主要是<code>reword</code>修改已经提交的<code>message</code>信息。</p>
<a id="more"></a>
<h2 id="一-修改已经-commit-但没有-push-的-commit-message"><a class="markdownIt-Anchor" href="#一-修改已经-commit-但没有-push-的-commit-message"></a> 一、修改已经 commit 但没有 push 的 commit message</h2>
<h3 id="1查看提交历史"><a class="markdownIt-Anchor" href="#1查看提交历史"></a> 1.查看提交历史</h3>
<p><code>git log --oneline -10</code></p>
<blockquote>
<p><code>--onlien</code>的方式能够显示精简的日志信息</p>
</blockquote>
<p>显示的信息[当前分支为<code>zzz</code>]：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">15af769 (HEAD -&gt; zzz) 10-15 通过模型自动写入时间戳[补充order模型隐藏字段的设置]</span><br><span class="line">197fcdd 10-13 测试下14 测试下单接口, 修改程序错误</span><br><span class="line">fdeb6af 10-13 一对多关系的新增操作[完成下单接口方法]</span><br><span class="line">da0bd4e 10-13 订单创建[添加订单信息到order order_product表]</span><br><span class="line">5ab5068 10-11 订单快照的实现</span><br><span class="line">09c2116 10-10 订单快照的业务分析</span><br><span class="line">8493571 10-9 下单接口说明文档补充注释</span><br><span class="line">6edda7e (origin/develop) 下单接口业务模型</span><br><span class="line">60b8f01 10-7 编写一个复杂的验证器</span><br><span class="line">3e8375c 10-4|5|6 下单与支付流程 + 重构权限控制前置方法</span><br></pre></td></tr></table></figure>
<p>发现提交的信息中：</p>
<ul>
<li>
<p><code>6edda7e</code>的信息中没有加标题序号</p>
</li>
<li>
<p><code>da0bd4e</code>的信息中标题序号错误</p>
</li>
<li>
<p><code>197fcdd</code>的信息中标题序号和内容有误</p>
</li>
</ul>
<h3 id="2通过git-rebase-i编辑提交的历史"><a class="markdownIt-Anchor" href="#2通过git-rebase-i编辑提交的历史"></a> 2.通过<code>git rebase -i</code>编辑提交的历史</h3>
<blockquote>
<p><code>git-rebase - Reapply commits on top of another base tip</code> [重新应用提交到另一个基础提示之上]</p>
</blockquote>
<p>在上面的日志中可以看到<code>6edda7e</code>为已经 push 的分支了，暂时不介绍这个，现在需要修改<code>da0bd4e</code>和 <code>197fcdd</code>两个提交的分支上的<code>message</code>内容。</p>
<p>（1）编辑最久远的需要修改的分支的前一个分支上</p>
<p><code>git rebase -i 60b8f01</code></p>
<p>显示的内容：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">pick 6edda7e 下单接口业务模型</span><br><span class="line">pick 8493571 10-9 下单接口说明文档补充注释</span><br><span class="line">pick 09c2116 10-10 订单快照的业务分析</span><br><span class="line">pick 5ab5068 10-11 订单快照的实现</span><br><span class="line">pick da0bd4e 10-13 订单创建[添加订单信息到order order_product表]</span><br><span class="line">pick fdeb6af 10-13 一对多关系的新增操作[完成下单接口方法]</span><br><span class="line">pick 197fcdd 10-13 测试下14 测试下单接口, 修改程序错误</span><br><span class="line">pick 15af769 10-15 通过模型自动写入时间戳[补充order模型隐藏字段的设置]</span><br></pre></td></tr></table></figure>
<p>并且下方会提示修改建议：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># Commands:</span></span><br><span class="line"><span class="comment"># p, pick &lt;commit&gt; = use commit</span></span><br><span class="line"><span class="comment"># r, reword &lt;commit&gt; = use commit, but edit the commit message</span></span><br><span class="line"><span class="comment"># e, edit &lt;commit&gt; = use commit, but stop for amending</span></span><br><span class="line"><span class="comment"># s, squash &lt;commit&gt; = use commit, but meld into previous commit</span></span><br><span class="line"><span class="comment"># f, fixup &lt;commit&gt; = like "squash", but discard this commit's log message</span></span><br><span class="line"><span class="comment"># x, exec &lt;command&gt; = run command (the rest of the line) using shell</span></span><br><span class="line"><span class="comment"># d, drop &lt;commit&gt; = remove commit</span></span><br><span class="line"><span class="comment"># l, label &lt;label&gt; = label current HEAD with a name</span></span><br><span class="line"><span class="comment"># t, reset &lt;label&gt; = reset HEAD to a label</span></span><br><span class="line"><span class="comment"># m, merge [-C &lt;commit&gt; | -c &lt;commit&gt;] &lt;label&gt; [# &lt;oneline&gt;]</span></span><br></pre></td></tr></table></figure>
<p>我们需要操作的是重新编辑已经提交的分支记录的 message 信息，所以对应的应该是<code>reword</code>,简写为<code>r</code>。</p>
<p>（2）修改显示的内容，将<code>pick</code>修改为<code>reword</code> [保留提交的分支记录，但是编辑提交的信息]</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">r 6edda7e 下单接口业务模型</span><br><span class="line">pick 8493571 10-9 下单接口说明文档补充注释</span><br><span class="line">pick 09c2116 10-10 订单快照的业务分析</span><br><span class="line">pick 5ab5068 10-11 订单快照的实现</span><br><span class="line">r da0bd4e 10-13 订单创建[添加订单信息到order order_product表]</span><br><span class="line">pick fdeb6af 10-13 一对多关系的新增操作[完成下单接口方法]</span><br><span class="line">r 197fcdd 10-13 测试下14 测试下单接口, 修改程序错误</span><br><span class="line">pick 15af769 10-15 通过模型自动写入时间戳[补充order模型隐藏字段的设置]</span><br></pre></td></tr></table></figure>
<p>将需要修改的记录前的 <code>pick</code> 改为 <code>r</code>，然后<code>:wq</code>保存退出后，会<strong>按顺序</strong>自动进入需要编辑的提交信息框</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">下单接口业务模型</span><br><span class="line"></span><br><span class="line"><span class="comment"># Please enter the commit message for your changes. Lines starting</span></span><br><span class="line"><span class="comment"># with '#' will be ignored, and an empty message aborts the commit.</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># Date:      Wed Aug 8 20:08:03 2018 +0800</span></span><br></pre></td></tr></table></figure>
<p>然后将第一行的提交信息修改为需要设置的信息，然后<code>:wq</code>保存退出，进入下一条需要编辑的提交记录。将全部需要修改的分支信息依次修改完成后，保存退出后会出现下面的信息，表示提交成功。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">[detached HEAD 91d973f] 10-8|9 下单接口业务模型</span><br><span class="line">Date: Wed Aug 8 20:08:03 2018 +0800</span><br><span class="line">3 files changed, 252 insertions(+), 1 deletion(-)</span><br><span class="line">create mode 100644 application/api/service/Order.php</span><br><span class="line">[detached HEAD b007935] 10-12 订单创建[添加订单信息到order order_product表]</span><br><span class="line">4 files changed, 179 insertions(+), 23 deletions(-)</span><br><span class="line">create mode 100644 application/api/model/Order.php</span><br><span class="line">create mode 100644 application/api/model/OrderProduct.php</span><br><span class="line">[detached HEAD a5449fc] 10-14 测试下单接口, 修改程序错误</span><br><span class="line">4 files changed, 99 insertions(+), 7 deletions(-)</span><br><span class="line">Successfully rebased and updated refs/heads/develop.</span><br></pre></td></tr></table></figure>
<p>再次执行<code>git log --oneline -10</code>命令后，即可看到分支的信息为修改后的提交信息</p>
<h2 id="二-修改已经-push-的-commmit-message"><a class="markdownIt-Anchor" href="#二-修改已经-push-的-commmit-message"></a> 二、修改已经 push 的 commmit message</h2>
<p>对于已经提交的信息的分支信息操作步骤同上，只是在推送<code>push</code>的时候需要加<code>--force</code>，强制覆盖远程分支上的提交信息。</p>

		</div>

		
			<div class="art-item-footer">
					
					
						<span class="art-item-right">下一篇文章：<a href="/2018/08/08/js-react/" rel="next"  title="【React入门】实现todolist功能">
							【React入门】实现todolist功能
						</a><i class="icon icon-chevron-thin-right"></i></span>
					
			</div>
		

		
			

    <div class="recommended_posts">
        <h3>推荐文章</h3>
        <ul>
            
                <li><a href="http://blog.i2arch.tech/2018/08/08/js-react/">【React入门】实现todolist功能</a></li>
            
                <li><a href="http://blog.i2arch.tech/2018/08/07/tp5-controller3/">【ThinkPHP5深入理解】控制器（三）-- 前置操作</a></li>
            
                <li><a href="http://blog.i2arch.tech/2018/07/31/docker-base/">【Docker】--入门使用</a></li>
            
                <li><a href="http://blog.i2arch.tech/2018/07/31/docker-install/">【Docker】--安装与配置</a></li>
            
        </ul>
    </div>


		
	</section>

	

</article>
<script>
	window.subData = {
		title: '【Git】修改已经提交的commit内容',
		tools: true
	}
</script>

      </div>

      <!-- 右侧边栏组件 -->
      <aside class='l_side'>
        
      </aside>

      <div class="gotop">
          
    <a href='javascript:void(0)'>
        <i class="icon icon-arrow_upward flat-box">
        </i>
    </a>

      </div>
      <script>setLoadingBarProgress(60);</script>
    </div>
  </div>
  <footer id="footer" class="clearfix">

	<div class="social-wrapper">
  	
      
        <a href="https://github.com/zqunor" class="social github"
          target="_blank" rel="external">
          <span class="icon icon-github"></span>
        </a>
      
        <a href="https://www.cnblogs.com/zqunor" class="social zhihu"
          target="_blank" rel="external">
          <span class="icon icon-zhihu"></span>
        </a>
      
        <a href="/atom.xml" class="social rss"
          target="_blank" rel="external">
          <span class="icon icon-rss"></span>
        </a>
      
    
  </div>
  <div>Theme <a href='https://github.com/stkevintan/hexo-theme-material-flow' class="codename">MaterialFlow</a>  Powered by<a href="https://blog.i2arch.tech/" target="_blank">程序小工</a>.</div>

</footer>

  <script>setLoadingBarProgress(80);</script>
  

    <script src="//apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <script src='//cdn.bootcss.com/node-waves/0.7.5/waves.min.js'></script>
    <script src="//cdn.bootcss.com/scrollReveal.js/3.3.2/scrollreveal.min.js"></script>
    <script src="/js/jquery.fitvids.js"></script>
      <script>
        var GOOGLE_CUSTOM_SEARCH_API_KEY = "";
        var GOOGLE_CUSTOM_SEARCH_ENGINE_ID = "";
        var ALGOLIA_API_KEY = "";
        var ALGOLIA_APP_ID = "";
        var ALGOLIA_INDEX_NAME = "";
        var AZURE_SERVICE_NAME = "";
        var AZURE_INDEX_NAME = "";
        var AZURE_QUERY_KEY = "";
        var BAIDU_API_ID = "fca5b83cf69f752ddc96a61ef64a3659";
        var SEARCH_SERVICE = "hexo";
        var ROOT = "/" || "/";
        if (!ROOT.endsWith('/')) ROOT += '/';
      </script>
      <script src="/js/search.js"></script>
        <script src="/js/app.js"></script>
  <script>setLoadingBarProgress(100);</script>
</body>
</html>
